package com.example.administrator.coldlain.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.administrator.coldlain.R;
import com.example.administrator.coldlain.adapter.ViewpagerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2019/3/25 0025.
 */

public class Fragment1 extends Fragment {
    private GridView gridview;

    List<HashMap<String, Object>> gridlist;
    View view;

    private List<View> list;
    private ViewPager mHomeViewpager;
    private LayoutInflater layoutInflater;
    private View view1,view2,view3;
    private int ids[] = {R.id.img_v1, R.id.img_v2, R.id.img_v3};
    private ImageView[] imageViews;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.layout1, container, false);
        initData();
        initView();
        initViewpager();
        return view;
    }
    private void initData() {
        gridlist = new ArrayList<>();
        HashMap<String, Object> hashMap;
        hashMap = new HashMap<>();
        hashMap.put("img", R.drawable.messenger);
        hashMap.put("title", "用户管理");
        gridlist.add(hashMap);
        hashMap = new HashMap<>();
        hashMap.put("img", R.drawable.weather);
        hashMap.put("title", "车间管理");
        gridlist.add(hashMap);
        hashMap = new HashMap<>();
        hashMap.put("img", R.drawable.world);
        hashMap.put("title", "设备管理");
        gridlist.add(hashMap);
        hashMap = new HashMap<>();
        hashMap.put("img", R.drawable.youtube);
        hashMap.put("title", "系统设置");
        gridlist.add(hashMap);
        hashMap = new HashMap<>();
        hashMap.put("img", R.drawable.camera);
        hashMap.put("title", "热点数据");
        gridlist.add(hashMap);
        hashMap = new HashMap<>();
        hashMap.put("img", R.drawable.mszn);
        hashMap.put("title", "数据统计");
        gridlist.add(hashMap);
    }

    private void initView() {
        gridview = (GridView) view.findViewById(R.id.gridview);
        SimpleAdapter simpleAdapter = new SimpleAdapter(getContext(), gridlist,
                R.layout.gridview_item,
                new String[]{"img", "title"}, new int[]{R.id.imageView, R.id.title});

        gridview.setAdapter(simpleAdapter);

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Toast.makeText(getContext(), "您即将进入用户管理功能!",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(getContext(), "您即将进入车间管理功能!",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(getContext(), "您即将进入设备管理功能!",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Toast.makeText(getContext(), "您即将进入系统设置功能!",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }
    private void initViewpager() {
        mHomeViewpager = (ViewPager) view.findViewById(R.id.home_viewpager);
        list=new ArrayList<>();
        layoutInflater=getLayoutInflater();
        view1=layoutInflater.inflate(R.layout.home_view_one,null);
        view2=layoutInflater.inflate(R.layout.home_view_two,null);
        view3=layoutInflater.inflate(R.layout.home_view_three,null);

        list.add(view1);
        list.add(view2);
        list.add(view3);
        mHomeViewpager.setAdapter(new ViewpagerAdapter(list));
        imageViews = new ImageView[list.size()];
        for (int i = 0; i < list.size(); i++) {
            imageViews[i] = view.findViewById(ids[i]);
        }
        mHomeViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < list.size(); i++) {
                    if (i == position)
                        imageViews[i].setImageResource(R.drawable.img_view);
                    else
                        imageViews[i].setImageResource(R.drawable.img_view2);
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
